package main;


import java.sql.Connection;
import dataBaseInteraction.DataProcessing;
import sensors.*;
import timer.Watch;
import timer.MainTimer;
import timer.TimerBean;

public class Simulator {

	public static Connection connection = null;
	
	public static WaterSensor senzorApaBuc=new WaterSensor();
	public static WaterSensor senzorApaBaie1=new WaterSensor();
	public static WaterSensor senzorApaBaie2=new WaterSensor();
	
	public static CurrentConsumptionSensor senzorCurent=new CurrentConsumptionSensor();
	
	public static GasSensor senzorGaz=new GasSensor();
	
	public static TemperatureSensor senzorTemperatura=new TemperatureSensor();
	
	public static MovementSensor senzorMiscareBucatarie=new MovementSensor();
	public static MovementSensor senzorMiscareBaie1=new MovementSensor();
	public static MovementSensor senzorMiscareBaie2=new MovementSensor();
	public static MovementSensor senzorMiscareLiving=new MovementSensor();
	public static MovementSensor senzorMiscareDormitor1=new MovementSensor();
	public static MovementSensor senzorMiscareDormitor2=new MovementSensor();
	public static MovementSensor senzorMiscareDormitor3=new MovementSensor();
	public static MovementSensor senzorMiscareHol=new MovementSensor();
	
	
	public Simulator()
	{
		connection=DataProcessing.connectToDB(DataProcessing.userName, DataProcessing.password, DataProcessing.url);
		DataProcessing.clearTable(connection, "scenariu");
		
	}
	
	/**
	 * porneste simulatorul
	 */
	public void startSimulator() throws InterruptedException
	{
		CreateScenarioPersons omuleti=new CreateScenarioPersons();
		TimerBean t=new TimerBean();
		/**
		 * ceasul curent
		 */
		Watch hcb=new Watch();
		hcb.setName("House hours");
		MainTimer mainTimer=new MainTimer();
				
		senzorApaBuc.setName("senzor apa bucatarie");
		senzorApaBaie1.setName("senzor apa baie 1");
		senzorApaBaie2.setName("senzor apa baie 2");
		
		senzorCurent.setName("senzor curent");
		
		senzorGaz.setName("senzor gaz");
		
		senzorTemperatura.setName("senzor temperatura");
		
		senzorMiscareBucatarie.setName("senzor miscare bucatarie");
		senzorMiscareBaie1.setName("senzor miscare baie 1");
		senzorMiscareBaie2.setName("senzor miscare baie 2");
		senzorMiscareLiving.setName("senzor miscare living");
		senzorMiscareDormitor1.setName("senzor miscare dormitor 1");
		senzorMiscareDormitor2.setName("senzor miscare dormitor 2");
		senzorMiscareDormitor3.setName("senzor miscare dormitor 3");
		senzorMiscareHol.setName("senzor miscare hol");
		
		senzorCurent.setPower(DataProcessing.getCurrentConsumption(connection, "electricitate", "frigider", "pornit si raceste").get(0));
		senzorTemperatura.setPower(20);

		//0:0:0
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorApaBaie1.getName(), Simulator.senzorApaBaie1.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorApaBaie2.getName(), Simulator.senzorApaBaie2.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorApaBuc.getName(), Simulator.senzorApaBuc.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorCurent.getName(), Simulator.senzorCurent.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorGaz.getName(), Simulator.senzorGaz.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorMiscareBaie1.getName(), Simulator.senzorMiscareBaie1.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorMiscareBaie2.getName(), Simulator.senzorMiscareBaie2.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorMiscareBucatarie.getName(), Simulator.senzorMiscareBucatarie.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorMiscareDormitor1.getName(), Simulator.senzorMiscareDormitor1.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorMiscareDormitor2.getName(), Simulator.senzorMiscareDormitor2.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorMiscareDormitor3.getName(), Simulator.senzorMiscareDormitor3.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorMiscareHol.getName(), Simulator.senzorMiscareHol.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorMiscareLiving.getName(), Simulator.senzorMiscareLiving.getPower());
		DataProcessing.insetIntoTable(Simulator.connection, "scenariu", "0:0:0", Simulator.senzorTemperatura.getName(), Simulator.senzorTemperatura.getPower());
		
		
		t.setRunning(true);
		t.addTimerListener(hcb);
		
		t.addTimerListener(mainTimer);
		
		t.addTimerListener(senzorApaBuc);
		t.addTimerListener(senzorApaBaie1);
		t.addTimerListener(senzorApaBaie2);
		
		t.addTimerListener(senzorCurent);
		
		t.addTimerListener(senzorTemperatura);
		
		t.addTimerListener(senzorGaz);
		
		t.addTimerListener(senzorMiscareBucatarie);
		t.addTimerListener(senzorMiscareBaie1);
		t.addTimerListener(senzorMiscareBaie2);
		t.addTimerListener(senzorMiscareLiving);
		t.addTimerListener(senzorMiscareDormitor1);
		t.addTimerListener(senzorMiscareDormitor2);
		t.addTimerListener(senzorMiscareDormitor3);
		t.addTimerListener(senzorMiscareHol);
	}
	
	/**
	 * opreste simulatorul
	 */
	public void stopSimulator()
	{
		if (connection!=null)
			DataProcessing.closeDB(connection);
	}
	
}
